/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.sf.odinms.net.channel.handler;

import java.util.Iterator;
import net.sf.odinms.client.Equip;
import net.sf.odinms.client.GameConstants;
import net.sf.odinms.client.IEquip;
import net.sf.odinms.client.IItem;
import net.sf.odinms.client.MapleCharacter;
import net.sf.odinms.client.MapleClient;
import net.sf.odinms.client.MapleInventory;
import net.sf.odinms.client.MapleInventoryType;
import net.sf.odinms.net.AbstractMaplePacketHandler;
import net.sf.odinms.net.channel.ChannelServer;
import net.sf.odinms.server.MapleItemInformationProvider;
import net.sf.odinms.tools.MaplePacketCreator;
import net.sf.odinms.tools.data.input.SeekableLittleEndianAccessor;

/**
 *
 * @author 千石抚子
 */
public final class 装备强化 extends AbstractMaplePacketHandler {

    public final void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
        System.out.println("装备强化："+slea.toString());
        MapleCharacter chr = c.getPlayer();
        slea.skip(4);
		byte slot = (byte)slea.readShort();
		byte dst = (byte)slea.readShort();
		slea.readByte();
		IEquip toScroll;
		if (dst < 0)
			toScroll = (IEquip)c.getPlayer().getInventory(MapleInventoryType.EQUIPPED).getItem(dst);
		else
			toScroll = (IEquip)c.getPlayer().getInventory(MapleInventoryType.EQUIP).getItem(dst);
		if (toScroll == null)
			return;
		MapleInventory useInventory = c.getPlayer().getInventory(MapleInventoryType.USE);
		IItem scroll = useInventory.getItem(slot);
		if (scroll.getQuantity() <= 0)
			return;
		IEquip.ScrollResult scrollSuccess = IEquip.ScrollResult.CURSE;
		String t = "装备属性没有任何变化.";
		switch (scroll.getItemId())
		{
		case 2049300:
		case 2049303:
		{
			int a = toScroll.getStarlevel();
			int newc = 10;
			switch (a)
			{
			case 0: // '\0'
			case 1: // '\001'
				newc = 100;
				break;

			case 2: // '\002'
				newc = 90;
				break;

			case 3: // '\003'
				newc = 80;
				break;

			case 4: // '\004'
				newc = 70;
				break;

			case 5: // '\005'
				newc = 60;
				break;

			case 6: // '\006'
				newc = 50;
				break;

			case 7: // '\007'
				newc = 40;
				break;

			case 8: // '\b'
				newc = 30;
				break;

			case 9: // '\t'
				newc = 20;
				break;
			}
			int chance = (int)(Math.random() * 100D);
			if (chance < newc)
			{
				t = StrengthenEquip(chr, (Equip)toScroll, toScroll.getStarlevel() + 1);
				toScroll.setStarlevel((byte) (toScroll.getStarlevel() + 1));
				scrollSuccess = IEquip.ScrollResult.SUCCESS;
				chr.dropMessage(6, t);
			} else
			{
				scrollSuccess = IEquip.ScrollResult.CURSE;
			}
			break;
		}

		case 2049301:
		{
			int a = toScroll.getStarlevel();
			int newc = 10;
			switch (a)
			{
			case 0: // '\0'
			case 1: // '\001'
				newc = 80;
				break;

			case 2: // '\002'
				newc = 70;
				break;

			case 3: // '\003'
				newc = 60;
				break;

			case 4: // '\004'
				newc = 50;
				break;

			case 5: // '\005'
				newc = 40;
				break;

			case 6: // '\006'
				newc = 30;
				break;

			case 7: // '\007'
				newc = 20;
				break;

			case 8: // '\b'
				newc = 10;
				break;
			}
			int chance = (int)(Math.random() * 100D);
			if (chance < newc)
			{
				t = StrengthenEquip(chr, (Equip)toScroll, toScroll.getStarlevel() + 1);
				toScroll.setIdentify((byte) (toScroll.getStarlevel() + 1));
				scrollSuccess = IEquip.ScrollResult.SUCCESS;
				chr.dropMessage(6, t);
			} else
			{
				scrollSuccess = IEquip.ScrollResult.CURSE;
			}
			break;
		}

		case 2049304:
		{
			int a = toScroll.getStarlevel();
			int chance = (int)(Math.random() * 100D);
			if (chance < 80)
			{
				for (int i = 0; i < 3; i++)
				{
					t = StrengthenEquip(chr, (Equip)toScroll, toScroll.getStarlevel() + 1);
					chr.dropMessage(6, t);
					toScroll.setStarlevel((byte) (toScroll.getStarlevel() + 1));
				}

				scrollSuccess = IEquip.ScrollResult.SUCCESS;
			} else
			{
				scrollSuccess = IEquip.ScrollResult.CURSE;
			}
			break;
		}

		case 2049305:
		{
			int a = toScroll.getStarlevel();
			int chance = (int)(Math.random() * 100D);
			if (chance < 60)
			{
				for (int i = 0; i < 4; i++)
				{
					t = StrengthenEquip(chr, (Equip)toScroll, toScroll.getStarlevel() + 1);
					chr.dropMessage(6, t);
					toScroll.setStarlevel((byte) (toScroll.getStarlevel() + 1));
				}

				scrollSuccess = IEquip.ScrollResult.SUCCESS;
			} else
			{
				scrollSuccess = IEquip.ScrollResult.CURSE;
			}
			break;
		}

		case 2049302:
		default:
		{
			return;
		}
		}
		useInventory.removeItem(scroll.getPosition(), (short)1, false);
		if (scrollSuccess == IEquip.ScrollResult.CURSE)
		{ 
            c.getSession().write(MaplePacketCreator.scrolledItem(scroll, toScroll, true));
            if (toScroll.getStarlevel() < 6);
				if (dst < 0)
					c.getPlayer().getInventory(MapleInventoryType.EQUIPPED).removeItem(toScroll.getPosition());
				else
					c.getPlayer().getInventory(MapleInventoryType.EQUIP).removeItem(toScroll.getPosition());
			
		} else
		{
			c.getSession().write(MaplePacketCreator.scrolledItem(scroll, toScroll, false));
			if (toScroll.getStarlevel() > 7)
			{
				for (Iterator i$ = ChannelServer.getAllInstances().iterator(); i$.hasNext();)
				{
					ChannelServer cserv1 = (ChannelServer)i$.next();
					while (i$.hasNext())
					{
						MapleCharacter mch = (MapleCharacter)i$.next();
						mch.startMapEffect((new StringBuilder()).append(c.getPlayer().getName()).append("的").append(MapleItemInformationProvider.getInstance().getName(toScroll.getItemId())).append("(+").append(toScroll.getLevel()).append(")强化到").append(toScroll.getStarlevel()).append("星成功了").toString(), 0x4e2025);
					}
				}

			}
		}
		c.getPlayer().getMap().broadcastMessage(MaplePacketCreator.潜能效果(true, c.getPlayer().getId(), scrollSuccess, false));
		c.getPlayer().equipChanged();
		c.getSession().write(MaplePacketCreator.enableActions());
	}
    public static final String StrengthenEquip(MapleCharacter chr, Equip nEquip, int xjlevel){
		int ss = xjlevel * 100;
		int gainc = (int)(Math.random() * (double)(1000 - ss));
		int gains = xjlevel + (int)(Math.random() * (double)xjlevel);
		String t = "装备属性没有任何变化.";
		if (nEquip.getTypeName().equals("Weapon"))
		{
			gainc = (int)(Math.random() * 1000D);
			if (GameConstants.isMagicWeapon(nEquip.getItemId()))
			{
				nEquip.setMatk((short)(nEquip.getMatk() + gains));
				//t = (new StringBuilder()).append("强化到").append(xjlevel).append("星，魔法攻击力(+").append(gains).append(")").toString();
			} else
			{
				nEquip.setWatk((short)(nEquip.getWatk() + gains));
				//t = (new StringBuilder()).append("强化到").append(xjlevel).append("星，物理攻击力(+").append(gains).append(")").toString();
			}
			return t;
		}
		if (nEquip.getTypeName().equals("Shoes"))
			if (gainc < 300)
			{
				nEquip.setSpeed((short)(nEquip.getSpeed() + gains));
				//t = (new StringBuilder()).append("强化到").append(xjlevel).append("星，移动速度(+").append(gains).append(")").toString();
			} else
			{
				nEquip.setJump((short)(nEquip.getJump() + gains));
				//t = (new StringBuilder()).append("强化到").append(xjlevel).append("星，跳跃力(+").append(gains).append(")").toString();
                return t;
			}
		if (gainc < 500)
		{
			gainc = (int)(Math.random() * 1000D);
			if (gainc < 250)
			{
				nEquip.setStr((short)(nEquip.getStr() + gains));
				//t = (new StringBuilder()).append("强化到").append(xjlevel).append("星，力量(+").append(gains).append(")").toString();
			} else
			if (gainc < 500)
			{
				nEquip.setDex((short)(nEquip.getDex() + gains));
				//t = (new StringBuilder()).append("强化到").append(xjlevel).append("星，敏捷(+").append(gains).append(")").toString();
			} else
			if (gainc < 750)
			{
				nEquip.setInt((short)(nEquip.getInt() + gains));
				//t = (new StringBuilder()).append("强化到").append(xjlevel).append("星，智力(+").append(gains).append(")").toString();
			} else
			{
				nEquip.setLuk((short)(nEquip.getLuk() + gains));
				//t = (new StringBuilder()).append("强化到").append(xjlevel).append("星，运气(+").append(gains).append(")").toString();
			}
		} else
		if (gainc < 800)
		{
			gainc = (int)(Math.random() * 1000D);
			if (gainc < 500)
			{
				nEquip.setAcc((short)(nEquip.getAcc() + gains));
				//t = (new StringBuilder()).append("强化到").append(xjlevel).append("星，命中率(+").append(gains).append(")").toString();
			} else
			{
				nEquip.setAvoid((short)(nEquip.getAvoid() + gains));
				//t = (new StringBuilder()).append("强化到").append(xjlevel).append("星，回避率(+").append(gains).append(")").toString();
			}
		} else
		{
			gainc = (int)(Math.random() * 1000D);
			if (gainc < 200)
			{
				nEquip.setHp((short)(nEquip.getHp() + gains));
				//t = (new StringBuilder()).append("强化到").append(xjlevel).append("星，HP(+").append(gains).append(")").toString();
			} else
			if (gainc < 400)
			{
				nEquip.setMp((short)(nEquip.getMp() + gains));
				//t = (new StringBuilder()).append("强化到").append(xjlevel).append("星，MP(+").append(gains).append(")").toString();
			} else
			if (gainc < 600)
			{
				nEquip.setWdef((short)(nEquip.getWdef() + gains));
				//t = (new StringBuilder()).append("强化到").append(xjlevel).append("星，物理防御力(+").append(gains).append(")").toString();
			} else
			if (gainc < 800)
			{
				nEquip.setMdef((short)(nEquip.getMdef() + gains));
				//t = (new StringBuilder()).append("强化到").append(xjlevel).append("星，魔法防御力(+").append(gains).append(")").toString();
			}
		}
        return t;
	}
}
